//
//  XYTPersonCenterViewController.m
//  XYTPayOrderSystem
//
//  Created by wupeng on 2017/8/23.
//  Copyright © 2017年 wupeng. All rights reserved.
//

#import "XYTPersonCenterViewController.h"
#import "XYTLoginRegisterViewController.h"
#import "XYTPersonModel.h"
@interface XYTPersonCenterViewController ()
//<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataSource;

@property (nonatomic, strong) UIView *headerView;

@property (nonatomic, strong) XYTPersonModel *personInfo;

@property (nonatomic, strong) UILabel *phoneLab;

@property (nonatomic, strong) UILabel *userTypeLab;

@property (nonatomic, strong) UILabel *moneyLab;

@end

@implementation XYTPersonCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    
    [self setupUI];
    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self loadData];
}

- (void)setupUI{
//    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStylePlain];
//    tableView.dataSource = self;
//    tableView.delegate = self;
//    tableView.tableHeaderView = self.headerView;
//    [self.view addSubview:tableView];
//    
//    
    
    UILabel *phoneLab = [[UILabel alloc] initWithFrame:CGRectMake(30, 100, kScreen_Width-30, 25)];
    phoneLab.textColor = [UIColor grayColor];
    phoneLab.font = [UIFont systemFontOfSize:14];
    phoneLab.text = @"手机号:";
    [self.view addSubview:phoneLab];
    self.phoneLab = phoneLab;
    
    UILabel *userTypeLab = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(phoneLab.frame)+20, kScreen_Width-30, 25)];
    userTypeLab.textColor = [UIColor grayColor];
    userTypeLab.font = [UIFont systemFontOfSize:14];
    userTypeLab.text = @"用户类型:";
    [self.view addSubview:userTypeLab];
    self.userTypeLab = userTypeLab;
    
    UILabel *moneyLab = [[UILabel alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(userTypeLab.frame)+20, kScreen_Width-30, 25)];
    moneyLab.textColor = [UIColor grayColor];
    moneyLab.font = [UIFont systemFontOfSize:14];
    moneyLab.text = @"账户余额:";
    [self.view addSubview:moneyLab];
    self.moneyLab = moneyLab;
    
    UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    loginBtn.frame = CGRectMake(30, kScreen_Height-40-50, kScreen_Width - 30*2, 40);
    loginBtn.backgroundColor = [UIColor colorWithRed:69.0f/255.0 green:140.0f/255.0 blue:200.0f/255.0 alpha:1];
    [loginBtn.layer setCornerRadius:2.0];
    loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [loginBtn setTitle:@"退出系统" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(sendLogout) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
}

- (void)loadData{
    if (![XYTUserInfoManager sharedManager].token) {//未登录时不去拉数据
        return;
    }
    [UIView showHUD:nil];
    NSString *signStr = [NSString stringWithFormat:@"%@%@",AppKey,AppSecret];
    NSString *md5Sign = [Md5Util md5:signStr];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params xyt_setObj:md5Sign forKey:@"sign"];
    XYTWeakSelf(self);
    [XYTNetRequest POST:GetUserInfoUrl parameters:params isNeedKey:NO success:^(id responseObject) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        XYTPersonModel *model = [XYTPersonModel mj_objectWithKeyValues:responseObject];
        strongSelf.personInfo = model;
        //保存用户信息
        [XYTUserInfoManager sharedManager].roleType = model.roleType;
        [[XYTUserInfoManager sharedManager] saveUserInfo];
        [self updateUI];
    } failure:^(NSString *errorMsg) {
        XYTStrongSelf(weakSelf)
        [UIView hideHUD];
        [strongSelf showAlertWithTitle:errorMsg];
        
    }];
}

- (void)updateUI{
    self.phoneLab.text = [NSString stringWithFormat:@"手机号:%@",self.personInfo.phone];
    self.userTypeLab.text = [NSString stringWithFormat:@"用户类型:%@",[self transferUserType:self.personInfo.roleType]];
    self.moneyLab.text = [NSString stringWithFormat:@"账户余额:%.2f",self.personInfo.accountMoney];
}

- (NSString *)transferUserType:(NSInteger )typeCode{
    NSString *userTypeStr = @"";
    switch (typeCode) {
        case 1:
            userTypeStr = @"管理员";
            break;
        case 2:
            userTypeStr = @"网点";
            break;
        case 3:
            userTypeStr = @"商户";
            break;
        case 4:
            userTypeStr = @"个人";
            break;
        case 5:
            userTypeStr = @"业务员";
            break;
            
        default:
            userTypeStr = @"未知";
            break;
    }
    return userTypeStr;
}

- (void)sendLogout{
    //tudo：暂时没有接口，server端不发请求，清空本地userinfo
    //tudo:以下操作理论上应该是server端退出登录成功之后回调处理
    [[XYTUserInfoManager sharedManager] loginOutAll];
//    //杀掉self 跳登录vc
    NSMutableArray *list = [NSMutableArray arrayWithArray:self.navigationController.viewControllers];
    [list removeObject:self];
    XYTLoginRegisterViewController *loginVC = [[XYTLoginRegisterViewController alloc] init];
    [list insertObject:loginVC atIndex:list.count];
    self.navigationController.viewControllers = [NSArray arrayWithArray:list];
}

#pragma mark - setter/getter
//- (UIView *)headerView{
//    if (!_headerView) {
//        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 150)];
//        _headerView.backgroundColor = [UIColor clearColor];
//    }
//    return _headerView;
//}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
